﻿using System;
using IniParser;
using IniParser.Model;

namespace SwitchIPModifier.Core
{
    /// <summary>
    /// 功能描述    ：IniHelper  
    /// 创 建 者    ：石斌
    /// 创建日期    ：2024/11/19 15:27:22 
    /// </summary>
    public static class IniHelper
    {
        private static string _filename = "config.ini";

        private static IniData _iniData;


        private static IniData GetIniData()
        {
            var parser = new FileIniDataParser();
            var iniData = parser.ReadFile(_filename);
            _iniData = iniData;
            return iniData;
        }

        private static KeyDataCollection GetIniDataSection(string section = "config")
        {
            var iniData = GetIniData();
            return iniData[section];
        }


        private static void Save()
        {
            var parser = new FileIniDataParser();
            parser.WriteFile(_filename, _iniData);
        }


        public static string Read(string key) => GetIniDataSection()[key];
        public static string Read(string section, string key) => GetIniDataSection(section)[key];

        public static void Write(string key, object value)
        {
            GetIniDataSection()[key] = value?.ToString() ?? "";
            Save();
        }
        public static void Write(string section, string key, object value)
        {
            GetIniDataSection(section)[key] = value?.ToString() ?? "";
            Save();
        }

        /// <summary>
        /// 获取配置实体
        /// </summary>
        /// <returns></returns>
        public static Config GetConfig()
        {
            var config = new Config();
            System.Reflection.PropertyInfo[] propertyInfos = config.GetType().GetProperties();
            foreach (var propertyInfo in propertyInfos)
            {
                var key = propertyInfo.Name;
                var value = Read(key) ?? "";
                var ptype = propertyInfo.PropertyType;
                if (string.IsNullOrEmpty(value)) continue;
                if (ptype == typeof(string))
                {
                    propertyInfo.SetValue(config, value, null);
                }
                if (ptype == typeof(int))
                {
                    propertyInfo.SetValue(config, int.Parse(value), null);
                }
                else if (ptype == typeof(int?) && int.TryParse(value, out var val))
                {
                    propertyInfo.SetValue(config, val, null);
                }
                else if (ptype == typeof(bool) && !string.IsNullOrEmpty(value))
                {
                    propertyInfo.SetValue(config, Convert.ChangeType(value, typeof(bool)), null);
                }
                else if (ptype == typeof(double) && !string.IsNullOrEmpty(value))
                {
                    propertyInfo.SetValue(config, Convert.ChangeType(value, typeof(double)), null);
                }
            }
            return config;
        }

        public static void SaveConfig(this Config config)
        {
            System.Reflection.PropertyInfo[] propertyInfos = config.GetType().GetProperties();
            foreach (var propertyInfo in propertyInfos)
            {
                IniHelper.Write(propertyInfo.Name, propertyInfo.GetValue(config)?.ToString());
            }
        }
    }
}
